package hkr.se1.budgetapp;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class RegisterFragment extends Fragment {

	private MainActivity activity;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.activity = (MainActivity) activity;

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		return inflater.inflate(R.layout.register_layout, container, false);
	}

	@Override
	public void onStart() {
		super.onStart();
		setupSpinner(activity);
		Button savebutton = (Button) activity.findViewById(R.id.save_button);
		savebutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				saveClick();

			}
		});

		Button clearbutton = (Button) activity.findViewById(R.id.clear_button);
		clearbutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				clearClick();

			}
		});
	}

	public void saveClick() {

		DatePicker datePicker = (DatePicker) activity
				.findViewById(R.id.datePicker);

		EditText noteField = (EditText) activity.findViewById(R.id.noteField);

		Spinner categoriesSpinner = (Spinner) activity
				.findViewById(R.id.categoriesSpinner);

		EditText amountField = (EditText) activity
				.findViewById(R.id.amountField);

		int month = datePicker.getMonth() + 1;
		String monthString;
		if (amountField.getText().toString().equals("")) {
			Toast.makeText(activity, "Please enter amount", Toast.LENGTH_SHORT).show();
		} else {
			if (month < 10) {
				monthString = "0" + month;
			} else {
				monthString = "" + month;
			}
			String date = datePicker.getYear() + "-" + monthString + "-"
					+ datePicker.getDayOfMonth();
			String note = noteField.getText().toString();
			String category = categoriesSpinner.getSelectedItem().toString();
			int amount = 0;
			if (category.equals("Income")) {
				amount = Integer.parseInt(amountField.getText().toString());
			} else {
				amount = -Integer.parseInt(amountField.getText().toString());
			}

			Entry entry = new Entry(date, note, category, amount);
			activity.EDS.saveEntry(entry);
			clearClick();
		}
	}

	public void clearClick() {
		EditText noteField = (EditText) activity.findViewById(R.id.noteField);
		EditText amountField = (EditText) activity
				.findViewById(R.id.amountField);

		noteField.setText("");
		amountField.setText("");
	}

	private void setupSpinner(Activity activity) {
		Spinner spinner = (Spinner) activity
				.findViewById(R.id.categoriesSpinner);
		ArrayAdapter<CharSequence> aAdapter = ArrayAdapter.createFromResource(
				activity, R.array.spinner_choices,
				android.R.layout.simple_spinner_item);
		aAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(aAdapter);
	}
}